package com.cqqs.controller;


import com.cqqs.common.bean.BaseRes;
import com.cqqs.controller.dto.req.project.ProjectPageReq;
import com.cqqs.controller.dto.req.project.ProjectSaveReq;
import com.cqqs.controller.dto.req.project.ProjectUpdateReq;
import com.cqqs.controller.dto.res.project.ProjectDetailRes;
import com.cqqs.controller.dto.res.project.ProjectPatentsRes;
import com.cqqs.controller.dto.res.project.ProjectPageRes;
import com.cqqs.page.TableDataInfo;
import com.cqqs.service.TechProjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 科技项目控制器
 *
 * @author mjxie
 * @date 2022/09/23
 */
@RestController
@Api(tags = "科技管理-项目管理")
@Slf4j
@RequestMapping("/project")
public class TechProjectController extends BaseController {

    private final TechProjectService techProjectService;


    @Autowired
    public TechProjectController(TechProjectService techProjectService) {
        this.techProjectService = techProjectService;
    }


    @ApiOperation(value = "新增项目")
    @PostMapping("")
    public BaseRes save(@Validated @RequestBody ProjectSaveReq projectSaveReq) {
        techProjectService.save(projectSaveReq);
        return new BaseRes<>();
    }

    @PutMapping("")
    @ApiOperation(value = "修改项目")
    public BaseRes update(@Validated @RequestBody ProjectUpdateReq projectUpdateReq) {
        techProjectService.update(projectUpdateReq);
        return new BaseRes<>();
    }


    /**
     * 查询项目详情
     *
     * @return 查询结果
     */
    @ApiOperation(value = "查询项目详情", notes = "查询项目详情")
    @GetMapping("/detail/{id}")
    public BaseRes<ProjectDetailRes> detail(@PathVariable Integer id) {
        return techProjectService.getDetail(id);
    }


    /**
     * 批量删除项目
     *
     * @param ids 主键ids
     * @return 删除结果
     */
    @ApiOperation(value = "批量删除项目")
    @DeleteMapping("")
    public BaseRes delete(@RequestBody Integer[] ids) {
        return techProjectService.delete(ids);
    }


    @ApiOperation(value = "分页项目")
    @ApiImplicitParams({@ApiImplicitParam(name = "pageNum", value = "页码 如1 2 3"),
            @ApiImplicitParam(name = "pageSize", value = "页码如 10 15 "),
            @ApiImplicitParam(name = "orderByColumn", value = "排序列字段名"),
            @ApiImplicitParam(name = "isAsc", value = "排序方式 升序=ascending/asc,降序=descending/desc ")})
    @GetMapping(value = "/page")
    public BaseRes<TableDataInfo<ProjectPageRes>> page(ProjectPageReq projectPageReq) {
        startPage();
        List<ProjectPageRes> list = techProjectService.getPage(projectPageReq);
        return new BaseRes<>(new TableDataInfo().getDataTable(list));
    }


    @ApiOperation(value = "查询依托项目的专利信息")
    @ApiImplicitParam(value = "项目ID", name = "projectId")
    @GetMapping("/getPatentsByProjectId/{projectId}")
    public BaseRes<ProjectPatentsRes> getPatentsByProjectId(@PathVariable Integer projectId) {
        return techProjectService.getPatentsByProjectId(projectId);
    }


}
